package com.yanqu.road.server.cmd.activity.commonteam;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.CommonTeamProto;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.ActivityTeamRankMgr;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeamActivity;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeamMgr;
import com.yanqu.road.server.manager.douluodalu.CrossDouLuoDaLuContext;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.FixSizeLinkedList;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C2_COMMON_TEAM_RECEIVE_TEAM_REWARD, desc = "通用组队-领取队伍榜单奖励")
public class CrossCommonTeamReceiveRankRewardCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }
    
    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossActivityProto.CrossGeneralActivityGetRewardReqMsg reqMsg = CrossActivityProto.CrossGeneralActivityGetRewardReqMsg.parseFrom(packet.getBytes());
        CommonTeamProto.CommonTeamGetRewardRespMsg.Builder respMsg = CommonTeamProto.CommonTeamGetRewardRespMsg.newBuilder();
        //获取活动
        CommonTeamActivity activity = CommonTeamMgr.getActivity(reqMsg.getActivityId(), groupId);
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_COMMON_TEAM_TEAM_ACTIVITY_NOT_EXIST);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_COMMON_TEAM_RECEIVE_TEAM_REWARD, respMsg));
            return;
        }
        //获取排名
        try {
            int rank = activity.getTeamRankByUserId(packet.getPlayerId(), reqMsg.getConditionType());
            CrossActivityProto.CrossGeneralActivityGetRewardRespMsg.Builder fromCrossRespMsg = CrossActivityProto.CrossGeneralActivityGetRewardRespMsg.newBuilder();
            fromCrossRespMsg.setActivityId(reqMsg.getActivityId());
            fromCrossRespMsg.setConditionType(reqMsg.getConditionType());
            fromCrossRespMsg.setConditionId(reqMsg.getConditionId());
            fromCrossRespMsg.setRank(rank);
            fromCrossRespMsg.setPosition(0);
            //返回游戏服
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(GameProtocol.S_COMMON_TEAM_RECEIVE_TEAM_REWARD_FROM_CROSS, fromCrossRespMsg);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), pbMsg);
            return;
        } catch (BusinessException e) {
            respMsg.setRet(e.getCode());
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_COMMON_TEAM_RECEIVE_TEAM_REWARD, respMsg));
    }

    @Override
    public void sendData() {}
}
